package contest.oop.sse.helpers;

import java.io.*;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Base64;

public class FileKeyLoaderHelper {
    public static KeyStore loadKeyStore(String keyStorePath, String keyStorePassword) throws IOException {
        InputStream inputStream = null;
        try {
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            inputStream = new FileInputStream(keyStorePath);
            keyStore.load(inputStream, keyStorePassword.toCharArray());
            return keyStore;
        } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return null;
    }

    public static String keyToString(Key key) {
        return Base64.getEncoder().encodeToString(key.getEncoded());
    }

    public static PrivateKey loadPrivateKey(KeyStore keyStore, String alias, String keyPassword) {
        KeyStore.PrivateKeyEntry privateKeyEntry;
        try {
            privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias,
                    new KeyStore.PasswordProtection(keyPassword.toCharArray()));
            return privateKeyEntry.getPrivateKey();
        } catch (UnrecoverableEntryException | NoSuchAlgorithmException | KeyStoreException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PublicKey loadPublicKey(KeyStore keyStore, String alias) {
        try {
            X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);
            return certificate.getPublicKey();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void generateKeyPairFile(KeyStore keyStore, String alias, String keyPassword, String keyFileDir) {
        PrivateKey privateKey = loadPrivateKey(keyStore, alias, keyPassword);
        PublicKey publicKey = loadPublicKey(keyStore, alias);
        if (privateKey != null && publicKey != null) {
            String privateKeyString = keyToString(privateKey);
            String publicKeyString = keyToString(publicKey);
            try (OutputStreamWriter writer = new FileWriter(keyFileDir + "/" + alias + ".key")) {
                writer.write(privateKeyString);
                writer.write("\n");
                writer.write(publicKeyString);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            throw new NullPointerException("Failed.");
        }
    }

    public static void main(String[] args) {
        KeyStore keyStore = null;
        try {
            keyStore = loadKeyStore("c:/users/paule/cpc.keystore", "cpc100");
            generateKeyPairFile(keyStore, "cpc", "cpc100", "c:/users/paule/");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
